<template>
  <el-dialog
    title="硬件快递物流信息"
    v-model="visible"
    top="10vh"
    width="80%"
    :before-close="handleClose"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
  >
    <el-row :gutter="40" class="expressInfo">
      <el-col :span="8">
        <el-card shadow="hover">
          <div class="label">状态：</div>
          <el-tag :type="defaultForm.state.type" effect="dark">
            {{ defaultForm.state.label }}
          </el-tag>
        </el-card>
      </el-col>
      <el-col :span="8">
        <el-card shadow="hover">
          <div class="label">快递公司：</div>
          <el-tag type="info" effect="dark">
            {{ defaultForm.courierCompanyName }}
          </el-tag>
        </el-card>
      </el-col>
      <el-col :span="8">
        <el-card shadow="hover">
          <div class="label">快递单号：</div>
          <el-tag type="info" effect="dark">
            {{ defaultForm.courierNumber }}
          </el-tag>
        </el-card>
      </el-col>
    </el-row>
    <el-timeline v-loading="loading">
      <el-timeline-item
        v-for="(item, index) in racesList"
        :key="index"
        :timestamp="item.AcceptTime">
        {{ item.AcceptStation }}
      </el-timeline-item>
    </el-timeline>
    <template #footer>
      <el-button @click="handleClose">关闭</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { ref, watch, type PropType } from 'vue'
import { UPDATE_MODEL_EVENT } from 'cm/contants'

import { getEquipmentExpress, type ICreateOrUpdateEquipment } from '@/api/equipment'

import { ElMessage } from 'element-plus'

const props = defineProps({
  modelValue: {
    type: Boolean,
    required: true,
    default: false
  },
  currId: {
    type: String,
    required: true,
    default: ''
  },
  courierCompanyName: {
    type: String,
    required: true,
    default: ''
  },
  courierNumber: {
    type: String,
    required: true,
    default: ''
  }
})

const emit = defineEmits([UPDATE_MODEL_EVENT, 'change'])

const visible = ref<boolean>(false)

const handleClose = () => {
  emit(UPDATE_MODEL_EVENT, false)
}

watch(() => props.modelValue,
  async () => {
    visible.value = props.modelValue
    if (visible.value && props.currId) {
      defaultForm.value.courierCompanyName  = props.courierCompanyName
      defaultForm.value.courierNumber  = props.courierNumber
      await sendLogisticsApi()
    }
  }
)
const loading = ref<boolean>(false)

const statusList = ref<Array<any>>([{
  type: '',
  label: '未发货',
  state: '1'
},{
  type: 'danger',
  label: '在途中',
  state: '2'
},{
  type: 'success',
  label: '已签收',
  state: '3'
},{
  type: 'warning',
  label: '问题件',
  state: '4'
}])

const defaultForm = ref({
  state: {
    type: '',
    label: '',
    state: ''
  },
  courierCompanyName: '',
  courierNumber: '',
})

const racesList = ref<Array<any>>([])
// 查询物流信息
const sendLogisticsApi = async () => {
  loading.value = true
  const res = await getEquipmentExpress(props.currId)
  loading.value = false
  if (res?.code === 200) {
    racesList.value = res.data?.Traces
    defaultForm.value.state = statusList.value.find((item: any) => item.state === res.data?.State)
    ElMessage({ type: 'success', message: '查询成功' })
  } else {
    ElMessage({ type: 'error', message: res?.msg || '网络异常，请稍后重试！' })
  }
}
</script>

<style lang="scss" scoped>
  .expressInfo {
    margin-bottom: 50px;

    .label {
      line-height: 24px
    }

    :deep(.el-card__body) {
      display: flex
    }
  }
</style>
